package com.lxj.student_security.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lxj.student_security.common.CommonResult;
import com.lxj.student_security.entity.LoginParam;
import com.lxj.student_security.entity.Permission;
import com.lxj.student_security.entity.Role;
import com.lxj.student_security.entity.User;
import com.lxj.student_security.entity.vo.UserRoleRelation;
import com.lxj.student_security.mapper.UserMapper;
import com.lxj.student_security.service.UserService;
import com.lxj.student_security.utils.JwtTokenUtil;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.List;

@Service
@CacheConfig(cacheNames = "login")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Override
    /**
     * 获取用户权限
     */
    public List<Permission> getPermissionsByUserId(Integer id) {
        return userMapper.getPermissionsByUserId(id);
    }

    @Override
    public String login(LoginParam loginParam) {
        String username = loginParam.getUsername();
        Assert.notNull(username, "用户名不能为空");
        String password = loginParam.getPassword();
        Assert.notNull(password, "密码不能为空");
        User user = userMapper.getByName(username);
        Assert.isTrue(user.getUsername() != null, "您输入的账户不存在");
        String password1 = user.getPassword();
        String encode = null;
        if (password.equals(password1)) {
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            encode = passwordEncoder.encode(password1);
        } else {
            encode = password1;
        }
        System.out.println(password1);
        boolean matches = passwordEncoder.matches(password, encode);
        if (matches) {
            return jwtTokenUtil.generateToken(user);
        }
        return null;
    }

    @Override
    public CommonResult findAll(User user) {
        PageHelper.startPage(user.getPageNum(), user.getPageSize());
        PageInfo<User> pageInfo = new PageInfo<>(this.userMapper.findAll(user));
        HashMap<String, Object> map = new HashMap<>();
        map.put("list", pageInfo.getList());
        map.put("number", pageInfo.getTotal());
        return CommonResult.success(map);
    }

    @Override
    public CommonResult add(User user) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(user.getPassword());
        user.setPassword(encode);
        int count = userMapper.add(user);
        if (count == 1) {
            return CommonResult.success(count);
        }
        return CommonResult.failed("添加失败！");
    }

    @Override
    public CommonResult delete(Integer[] id) {
        int result = userMapper.delete(id);
        if (result == 0) {
            return CommonResult.failed();
        }
        return CommonResult.success(result);
    }

    @Override
    public CommonResult findRole(Integer id) {
        List<UserRoleRelation> list = userMapper.findRole(id);
        return CommonResult.success(list);
    }

    @Override
    @Transactional
    public CommonResult addRole(UserRoleRelation urr) {
        updateUserType(urr);
        int count = userMapper.addRole(urr);
        Assert.notNull(count,"添加失败");
        return CommonResult.success(count);
    }

    @Override
    public CommonResult deleteRole(UserRoleRelation urr) {
        int count = userMapper.deleteRole(urr);
        Assert.notNull(count,"删除失败");
        return CommonResult.success(count);
    }

    public void updateUserType(UserRoleRelation urr){
        userMapper.updateUserType(urr);
    }
}
